{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.Wilcoxon-Mann-Whitney秩和检验"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "\tWilcoxon rank sum exact test\n",
       "\n",
       "data:  h and l\n",
       "W = 62, p-value = 0.1003\n",
       "alternative hypothesis: true location shift is not equal to 0\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "h=c(134,146,104,119,124,161,107,83,113,129,97,123)\n",
    "l=c(70,118,101,85,112,132,94)\n",
    "wilcox.test(h,l)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.Kruskal-Wallis单因素方差分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "ename": "ERROR",
     "evalue": "Error in parse(text = x, srcfile = src): <text>:1:15: unexpected '['\n1: drug=np.array([\n                  ^\n",
     "output_type": "error",
     "traceback": [
      "Error in parse(text = x, srcfile = src): <text>:1:15: unexpected '['\n1: drug=np.array([\n                  ^\nTraceback:\n"
     ]
    }
   ],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3. $r\\times s$列联表和$\\chi^2$独立性检验"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "\tPearson's Chi-squared test\n",
       "\n",
       "data:  d1\n",
       "X-squared = 15.073, df = 6, p-value = 0.01969\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "library(vcd)\n",
    "dataO=c(98,38,289)\n",
    "dataA=c(67,41,262)\n",
    "dataB=c(13,8,57)\n",
    "dataAB=c(18,12,30)\n",
    "d1=rbind(dataO,dataA,dataB,dataAB)\n",
    "d=data.frame(dataO,dataA,dataB,dataAB)\n",
    "chisq.test(d1,correct=T)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4. Mantel-Haenszel检验"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "\tMantel-Haenszel chi-squared test with continuity correction\n",
       "\n",
       "data:  x\n",
       "Mantel-Haenszel X-squared = 21.944, df = 1, p-value = 2.807e-06\n",
       "alternative hypothesis: true common odds ratio is not equal to 1\n",
       "95 percent confidence interval:\n",
       " 2.080167 6.099585\n",
       "sample estimates:\n",
       "common odds ratio \n",
       "         3.562044 \n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<style>\n",
       ".list-inline {list-style: none; margin:0; padding: 0}\n",
       ".list-inline>li {display: inline-block}\n",
       ".list-inline>li:not(:last-child)::after {content: \"\\00b7\"; padding: 0 .5ex}\n",
       "</style>\n",
       "<ol class=list-inline><li>2</li><li>2</li><li>2</li></ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item 2\n",
       "\\item 2\n",
       "\\item 2\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. 2\n",
       "2. 2\n",
       "3. 2\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       "[1] 2 2 2"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "HA=matrix(c(50,92,15,90),2)\n",
    "HB=matrix(c(47,5,135,60),2)\n",
    "m=c(HA,HB)\n",
    "x=array(m,c(2,2,2))\n",
    "mantelhaen.test(x)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 5. Spearman秩相关检验"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Warning message in cor.test.default(x, y, method = \"spearman\"):\n",
      "\"Cannot compute exact p-value with ties\"\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "\n",
       "\tSpearman's rank correlation rho\n",
       "\n",
       "data:  x and y\n",
       "S = 65.227, p-value = 0.003265\n",
       "alternative hypothesis: true rho is not equal to 0\n",
       "sample estimates:\n",
       "      rho \n",
       "0.7719346 \n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "x=c(65,79,67,66,89,85,84,73,88,80,86,75)\n",
    "y=c(62,66,50,68,88,86,64,62,92,64,81,80)\n",
    "cor.test(x,y,method='spearman')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "X1=c(41,43,39.5,38,40.5,41,40,38.5,44,39)\n",
    "X2=c(55.7,56.3,54.5,54.2,55.1,55.4,54.5,54.2,56.9,54.5)\n",
    "X3=c(8.6,9.2,8,5.6,6.8,8,8.6,7.4,9.8,7.4)\n",
    "?cor.test"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "R",
   "language": "R",
   "name": "ir"
  },
  "language_info": {
   "codemirror_mode": "r",
   "file_extension": ".r",
   "mimetype": "text/x-r-source",
   "name": "R",
   "pygments_lexer": "r",
   "version": "4.0.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
